package com.platform.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.StringUtils;
import com.platform.common.utils.bean.BeanUtils;
import com.platform.common.utils.uuid.SnowflakeIdWorker;
import com.platform.common.utils.uuid.UUID;
import com.platform.system.domain.dto.device.WebUserInfoDto;
import com.platform.system.domain.entity.device.WebUserInfo;
import com.platform.system.domain.vo.device.WebUserInfoVo;
import com.platform.system.mapper.device.WebUserInfoMapper;
import com.platform.system.service.ISysWebUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


/**
 * @author Huangkai
 */
@Slf4j
@Service
public class SysWebUserServiceImpl extends ServiceImpl<WebUserInfoMapper, WebUserInfo> implements ISysWebUserInfoService {

    @Resource
    WebUserInfoMapper webUserInfoMapper;

    @Resource
    ISysWebUserInfoService sysWebUserInfoService;

    @Override
    public WebUserInfoDto selectWebUser(WebUserInfoVo webUserInfoVo) {
        LambdaQueryWrapper<WebUserInfo> webUserInfoLambdaQueryWrapper = new QueryWrapper<WebUserInfo>().lambda();
        webUserInfoLambdaQueryWrapper.eq(StringUtils.isNotEmpty(webUserInfoVo.getPhone()), WebUserInfo::getPhone, webUserInfoVo.getPhone());
        webUserInfoLambdaQueryWrapper.eq(StringUtils.isNotEmpty(webUserInfoVo.getUid()), WebUserInfo::getUid, webUserInfoVo.getUid());
        WebUserInfo webUserInfo = webUserInfoMapper.selectOne(webUserInfoLambdaQueryWrapper);
        if(webUserInfo != null) {
            WebUserInfoDto webUserInfoDto = new WebUserInfoDto();
            BeanUtils.copyProperties(webUserInfo, webUserInfoDto);
            return webUserInfoDto;
        }
        return null;
    }

    @Override
    public Boolean insertWebUser(WebUserInfoVo webUserInfoVo) {
        WebUserInfo webUserInfo = new WebUserInfo();
        webUserInfo.setUid(SnowflakeIdWorker.getSnowflakeIdWorker());
        webUserInfo.setPhone(webUserInfoVo.getPhone());
        webUserInfo.setOpenId(webUserInfoVo.getOpenId());
        webUserInfo.setNick(StringUtils.getRandomName(6));
        webUserInfo.setCreateTime(new Date());
        webUserInfo.setUpdateTime(new Date());
        return webUserInfo.insert();
    }

    @Override
    public AjaxResult updateWebUser(WebUserInfoVo webUserInfoVo) {
        log.info("更新客户端用户{}", webUserInfoVo);
        WebUserInfoDto webUserInfoDto = sysWebUserInfoService.selectWebUser(webUserInfoVo);
        if(webUserInfoDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        LambdaQueryWrapper<WebUserInfo> webUserInfoLambdaQueryWrapper = new QueryWrapper<WebUserInfo>().lambda();
        webUserInfoLambdaQueryWrapper.eq(WebUserInfo::getUid, webUserInfoVo.getUid());
        WebUserInfo webUserInfo = new WebUserInfo();
        BeanUtils.copyProperties(webUserInfoVo, webUserInfo);
        webUserInfo.setUpdateTime(new Date());
        webUserInfo.update(webUserInfoLambdaQueryWrapper);
        return AjaxResult.success();
    }
}
